package com.emm.yixun.jobs.common;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class HttpUtil {
    public static String doPost(String url, String content, String charset){
    	String result = "";
    	HttpPost httpPost = new HttpPost(url);
    	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        try {
	   	StringEntity entity = new StringEntity(content,charset);
	   	entity.setContentEncoding(charset);
	        entity.setContentType("application/json");
	        httpPost.setEntity(entity);
	        HttpResponse httpResult = httpClient.execute(httpPost);
	        int statusCode = httpResult.getStatusLine().getStatusCode();
	        if (statusCode == HttpStatus.SC_OK) {
	        	result = EntityUtils.toString(httpResult.getEntity(),charset);
	        }
	        httpClient.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }  
    
    public static String readTxtFile(String filePath){
	StringBuffer bf = new StringBuffer();
        try {
                String encoding="GBK";
                File file=new File(filePath);
                if(file.isFile() && file.exists()){ //判断文件是否存在
                    InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file),encoding);//考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while((lineTxt = bufferedReader.readLine()) != null){
                	bf.append(lineTxt);
                    }
                    read.close();
        }else{
            System.out.println("找不到指定的文件");
        }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return bf.toString();
    } 
    public static void main(String[] args) throws Exception{
	String result = readTxtFile("C:\\Users\\Administrator\\Desktop\\params.txt");
	result=result.replace(" ", "");
	doPost("http://221.226.135.42:8589/flow/report/insert", result, "UTF-8");
    }
}
